/**
 * product catalog will use this for initialization
 * @author yuji kuzuhara
 */

import java.util.ArrayList;
import java.lang.String;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ProductReader{
  private BufferedReader source;
  private String nextline;

  //read products.txt
  public ProductReader(String productFile) throws IOException{
    source = new BufferedReader(new FileReader(productFile));
    nextline = source.readLine();
  }
  
  //return true if product file has more products
  public boolean hasMoreProducts(){
    if(nextline==null||nextline.length()==0)
      return false;
    else
      return true;
  }

  //return next product with: UPC, description, price
  public Product getNextProduct() throws IOException{
    if(hasMoreProducts()){
      Product p = new Product(nextline.substring(0,4),nextline.substring(9,29),Float.parseFloat(nextline.substring(34)));
      nextline = source.readLine();
      return p;
    }
    else{
      source.close();
      return null;
    }
  }
}

